/**
 * Copyright (c) 2018 电行世界 All rights reserved.
 *
 * http://gitlab.dev.cedte.com
 *
 * 版权所有，侵权必究！
 */

package com.base.commons.tools.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Redis工具类
 *
 * @author szxyidao@gmail.com
 * @since 1.0.0
 */
@Component
public class RedisUtils {
//    @Resource
    @Autowired(required = false)
    private RedisTemplate<String, Object> redisTemplate;

    /**  默认过期时长为24小时，单位：秒 */
    public final static long DEFAULT_EXPIRE = 60 * 60 * 24L;
    /**  过期时长为1小时，单位：秒 */
    public final static long HOUR_ONE_EXPIRE = 60 * 60 * 1L;
    /**  过期时长为6小时，单位：秒 */
    public final static long HOUR_SIX_EXPIRE = 60 * 60 * 6L;
    /**  不设置过期时长 */
    public final static long NOT_EXPIRE = -1L;

    public void set(String key, Object value, long expire){
        redisTemplate.opsForValue().set(key, value);//操作字符串
        if(expire != NOT_EXPIRE){
            expire(key, expire);
        }
    }

    public void set(String key, Object value){
        set(key, value, DEFAULT_EXPIRE);
    }

    public Object get(String key, long expire) {
        Object value = redisTemplate.opsForValue().get(key);
        if(expire != NOT_EXPIRE){
            expire(key, expire);
        }
        return value;
    }

    public Object get(String key) {
        return get(key, NOT_EXPIRE);
    }

    public Set<String> keys(String pattern){
        return redisTemplate.keys(pattern);
    }

    public void deleteByPattern(String pattern) {
        redisTemplate.delete(keys(pattern));
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }

    public void delete(Collection<String> keys) {
        redisTemplate.delete(keys);
    }

    public Object hGet(String key, String field) {
        return redisTemplate.opsForHash().get(key, field);
    }

    public Map<String, Object> hGetAll(String key){
        HashOperations<String, String, Object> hashOperations = redisTemplate.opsForHash();
        return hashOperations.entries(key);
    }

    public void hMSet(String key, Map<String, Object> map){
        hMSet(key, map, DEFAULT_EXPIRE);
    }

    public void hMSet(String key, Map<String, Object> map, long expire){
        redisTemplate.opsForHash().putAll(key, map); //hashMap

        if(expire != NOT_EXPIRE){
            expire(key, expire);
        }
    }

    public void hSet(String key, String field, Object value) {
        hSet(key, field, value, DEFAULT_EXPIRE);
    }

    public void hSet(String key, String field, Object value, long expire) {
        redisTemplate.opsForHash().put(key, field, value);

        if(expire != NOT_EXPIRE){
            expire(key, expire);
        }
    }

    public void expire(String key, long expire){
        redisTemplate.expire(key, expire, TimeUnit.SECONDS);
    }

    public void hDel(String key, Object... fields){
        redisTemplate.opsForHash().delete(key, fields);
    }

    public void leftPush(String key, Object value){
        leftPush(key, value, DEFAULT_EXPIRE);
    }

    public void leftPush(String key, Object value, long expire){
        redisTemplate.opsForList().leftPush(key, value);

        if(expire != NOT_EXPIRE){
            expire(key, expire);
        }
    }

    public Object rightPop(String key){
        return redisTemplate.opsForList().rightPop(key);
    }

    /**
     * @author szxyidao@hotmail.com
     * @description 获取key失效时间这里取毫秒
     * @date 11:54 2019-04-04
     * @param
     * @return long
     **/
    public long getExpire(String key){
        return redisTemplate.getExpire(key);
    }

    /**
     * 设置incrkey
     * @param key
     * @param data
     */
    public Long incrKey(String key,long data){
        return redisTemplate.opsForValue().increment(key,data);
    }

    /**
     * 设置增长指定值的incrkey
     * @param key
     * @param incrByValues
     * @param seconds
     */
    public Long incrByKey(String key,long incrByValues,long seconds){
        long inc = redisTemplate.opsForValue().increment(key,incrByValues);
        expire(key,seconds);
        return inc;
    }
    public Boolean setIfAbsent(String key, Object Values){
       return redisTemplate.opsForValue().setIfAbsent(key,Values);
    }
    public Object getAndSet(String key, Object Values){
        return redisTemplate.opsForValue().getAndSet(key,Values);
    }


    public void addSet(String key, List<Long> values, long expire){
        redisTemplate.opsForSet().add(key,values.toArray(new Object[values.size()]));
        if(expire != NOT_EXPIRE){
            expire(key, expire);
        }
    }

    public Set<Object> getSet(String key){
        return redisTemplate.opsForSet().members(key);
    }

    public boolean isMember(String key, Object value){
        return redisTemplate.opsForSet().isMember(key,value);
    }
}